package com.example.demo.app

import android.app.Application
import android.content.Context
import android.content.res.Configuration
import com.example.demo.utils.LocalManageUtil
import com.github.jokar.multilanguages.library.MultiLanguage

class MultiLanguagesApp : Application() {
    override fun attachBaseContext(base: Context) {
        // 第一次进入app时，保存系统选择语言
        LocalManageUtil.saveSystemCurrentLanguage(base)
        super.attachBaseContext(MultiLanguage.setLocal(base))
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        // 系统设置页面。保存系统选择语言
        LocalManageUtil.saveSystemCurrentLanguage(applicationContext, newConfig)
        MultiLanguage.onConfigurationChanged(applicationContext)
    }

    override fun onCreate() {
        super.onCreate()
        MultiLanguage.init { context -> LocalManageUtil.getSetLanguageLocale(context) }
        MultiLanguage.setApplicationLanguage(this)
    }
}